package com.cpi.cornpp.version.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.version.dto.MonthPeriodVersionDTO;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.view.MonthPeriodVersionView;

/**
 * Month Period Version Converter
 * @author Edgar Joao
 *
 */
public class MonthPeriodVersionConverter {
	
	private static final String SPACE = " ";	
	/**
	 * Month Period Version Converter DTO to View
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 */
	public MonthPeriodVersionView converterPeriodVersionDtoToView(MonthPeriodVersionDTO dto) throws VersionException{
		MonthPeriodVersionView monthPeriodVersionView = null;
		StringBuffer stringBufferDescription = null;
		try{
			if(dto != null){
				monthPeriodVersionView = new MonthPeriodVersionView();
				stringBufferDescription = new StringBuffer(0);
				monthPeriodVersionView.setId(dto.getId());
				stringBufferDescription.append(dto.getShortName());
				stringBufferDescription.append(SPACE);
				stringBufferDescription.append(dto.getYear());
				monthPeriodVersionView.setDescription(stringBufferDescription.toString());
			}
		}catch( Exception exception ){
			VersionException versionException=null;
			exception.printStackTrace(System.out);
			versionException= new VersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionException;
		}	
		return monthPeriodVersionView;
	}
	/**
	 * @author Edgar Joao
	 * @param periodVersionDTOs
	 * @return
	 * @throws VersionException
	 */
	public List convertDTOsToViews(List periodVersionDTOs) throws VersionException{
		List listOfPeriodVersionsDTOs = null;
		Iterator iterator = null;
		MonthPeriodVersionDTO monthPeriodVersionDTO = null;
		MonthPeriodVersionView monthPeriodVersionView = null;		
		try{
			listOfPeriodVersionsDTOs = new ArrayList(0);
			iterator = periodVersionDTOs.iterator();
			while( iterator.hasNext() ){
				monthPeriodVersionDTO =  (MonthPeriodVersionDTO) iterator.next();
				monthPeriodVersionView = converterPeriodVersionDtoToView(monthPeriodVersionDTO);
				listOfPeriodVersionsDTOs.add(monthPeriodVersionView);
			}						
		}catch(VersionException versionException){
			throw versionException;
		}catch( Exception exception ){
			VersionException versionException=null;
			exception.printStackTrace(System.out);
			versionException= new VersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw versionException;
		}
		return listOfPeriodVersionsDTOs;
	}
}
